package com.nkzy.controller;

import cn.hutool.core.bean.BeanUtil;
import com.nkzy.domain.dto.EmpFormDTO;
import com.nkzy.domain.pojo.Emp;
import com.nkzy.domain.pojo.Result;
import com.nkzy.domain.vo.EmpVO;
import com.nkzy.service.EmpService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;
    @PostMapping
    @Operation(summary = "保存员工信息")
    public Result saveEmp(@Valid @RequestBody EmpFormDTO  empFormDTO){
        Emp emp= BeanUtil.copyProperties(empFormDTO,Emp.class);
        empService.save(emp);
        return Result.success();
    }
    @GetMapping("/{id}")
    @Operation(summary = "查询一个员工")
    public Result getEmp(@PathVariable Integer id){
        Emp  emp=empService.getById(id);
        EmpVO userVO=BeanUtil.copyProperties(emp,EmpVO.class);
        return Result.success(userVO);
    }
    @PutMapping
    @Operation(summary = "修改一个员工")
    public Result updateEmp(@RequestBody EmpFormDTO  empFormDTO){
        Emp emp=BeanUtil.copyProperties(empFormDTO,Emp.class);
        empService.updateById(emp);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    @Operation(summary = "删除一个员工")
    public Result deleteEmp(@PathVariable Integer id){
        empService.removeById(id);
        return Result.success();
    }
    @GetMapping
    @Operation(summary = "查询所有员工")
    public Result getAllEmps(){
        List<Emp> emps=empService.list();
        List<EmpVO> empVOS=BeanUtil.copyToList(emps, EmpVO.class);
        return Result.success(empVOS);
    }
}
